<?php


/**
 * 会员筛选器操作类
 * @package  htdocs
 * @subpackage  module
 * @version 1.0
 */

class cSelectorHandler {

    /**
     * @var object 数据库实例
     */
    private static $dbca;

    /**
     * @var int 筛选器ID
     */
    private static $selectID;

    /**
     * @var int 会员号
     */
    private static $pcustID;


    public function __construct( $dbca ) {
        self::$dbca = $dbca;
    }


    /**
     * 查询筛选器信息
     * @return bool
     */
    private function searchSelector() {

        $search_sql = 'SELECT * FROM cust_selector WHERE selectID = ?';
        $selector_array = self::$dbca->stmt_query($search_sql, 'i', self::$selectID);
        if (!$selector_array)
            return false;

        return true;

    }


    /**
     * 查询筛选器选择信息
     * @return mixed
     */
    private function searchSelectorDetail() {

        $search_sql = 'SELECT * FROM cust_selector_detail WHERE selectID = ?';
        $selector_detail_array = self::$dbca->stmt_query($search_sql, 'i', self::$selectID);
        if (!$selector_detail_array)
            return null;

        $item_array = null;

        foreach($selector_detail_array as &$item) {
            if (empty($item->selector))
                continue;
            $item_array[$item->selectType][] = $item;
        }

        return $item_array;

    }


    /**
     * 检查商品是否符合会员筛选器
     * @param int $selectID 筛选器ID
     * @param int $pcustID 会员号
     * @return bool
     */
    public function isValid( $selectID, $pcustID ) {

        if (!isset($selectID) || !is_numeric($selectID))
            return true;

        if (!isset($pcustID) || !is_numeric($pcustID))
            return false;

        self::$selectID = $selectID;
        self::$pcustID = $pcustID;

        //查询筛选器信息
        if (!$this->searchSelector())
            return false;

        return $this->fitSelector();

    }


    /**
     * 检查会员是否匹配筛选器选项
     * @return bool
     */
    private function fitSelector() {

        try {

            //查询筛选器选择信息
            $selector_detail_array = $this->searchSelectorDetail();
            if (!$selector_detail_array)
                return false;

            //查询会员
            $customer_obj = new entity('customer_master');
            $customer_obj->pcustID = self::$pcustID;
            if (!self::$dbca->fetch($customer_obj))
                return false;

            //检查商品是否符合筛选器选项
            foreach($selector_detail_array as $key => &$detail) {

                switch($key) {

                    case 1: //分类筛选

                        foreach ($detail as &$item) {
                            $rank_array = explode(',', $detail->selector);
                            if (in_array($customer_obj->rankID, $rank_array)) {
                                if ($item->ruleFLg == 0)
                                    return false;
                            } else {
                                if ($item->ruleFLg == 1)
                                    return false;
                            }
                        }

                        break;

                }

            }

            return true;

        } catch (Exception $e) {
            error_log($e->getMessage());
            return false;
        }

    }


}